package kr.hkit.summury_java.erp.erp;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.summury_java.erp.dao.CodeTitleDAO;
import kr.hkit.summury_java.erp.dao.DepartmentDAO;
import kr.hkit.summury_java.erp.dao.EmployeeDAO;
import kr.hkit.summury_java.erp.dto.CodeTitle;
import kr.hkit.summury_java.erp.dto.Department;
import kr.hkit.summury_java.erp.dto.Employee;
import kr.hkit.summury_java.erp.garbage.PanelCodeTitleListUI;
import kr.hkit.summury_java.erp.garbage.PanelDepartmentListUI;
import kr.hkit.summury_java.erp.garbage.PanelEmployeeListUI;
import kr.hkit.summury_java.erp.serviceUI.PanelCodeTitleInputUI;
import kr.hkit.summury_java.erp.serviceUI.PanelDepartmentInputUI;
import kr.hkit.summury_java.erp.serviceUI.PanelEmployeeInputUI;
import kr.hkit.summury_java.erp.serviceUI.PanelListUI;

import java.awt.Toolkit;

public class FullMain extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu menuEmp;
	private JMenuItem addItem;
	private JMenuItem updateItem;
	private JMenuItem deleteItem;
	
	private JMenu menuDept;
	private JMenuItem searchItem;
	private JMenuItem addDItem;
	private JMenuItem updateDItem;
	private JMenuItem deleteDItem;
	private JMenuItem searchDItem;
	
	private JMenuItem listDItem;
	private JMenuItem listEItem;
	private JMenu menuTitle;
	private JMenuItem addMItem;
	private JMenuItem updateMItem;
	private JMenuItem deleteMItem;
	private JMenuItem searchMItem;
	private JMenuItem listMItem;
	
	Connection con = ConnectionFactory.getConnection();
	DepartmentDAO deptDao = new DepartmentDAO(con);
	EmployeeDAO empDao = new EmployeeDAO(con);
	CodeTitleDAO ctDao = new CodeTitleDAO(con);
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FullMain frame = new FullMain();
					frame.setVisible(true);
					frame.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FullMain() {
		initialize();
	}
	private void initialize() {
		setTitle("ERP System");
		setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\M201_08\\Desktop\\android.jpg"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		menuEmp = new JMenu("사원 정보");
		menuEmp.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuBar.add(menuEmp);
		
		addItem = new JMenuItem("사원 추가");
		addItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		addItem.addActionListener(this);
		menuEmp.add(addItem);
		
		updateItem = new JMenuItem("사원 수정");
		updateItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		updateItem.addActionListener(this);
		menuEmp.add(updateItem);
		
		deleteItem = new JMenuItem("사원 삭제");
		deleteItem.addActionListener(this);
		deleteItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuEmp.add(deleteItem);
		
		searchItem = new JMenuItem("사원 검색");
		searchItem.addActionListener(this);
		searchItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuEmp.add(searchItem);
		
		listEItem = new JMenuItem("사원 목록");
		listEItem.addActionListener(this);
		listEItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuEmp.add(listEItem);
		
		menuDept = new JMenu("부서 정보");
		menuDept.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuBar.add(menuDept);
		
		addDItem = new JMenuItem("부서 추가");
		addDItem.addActionListener(this);
		addDItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuDept.add(addDItem);
		
		updateDItem = new JMenuItem("부서 수정");
		updateDItem.addActionListener(this);
		updateDItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuDept.add(updateDItem);
		
		deleteDItem = new JMenuItem("부서 삭제");
		deleteDItem.addActionListener(this);
		deleteDItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuDept.add(deleteDItem);
		
		searchDItem = new JMenuItem("부서 검색");
		searchDItem.addActionListener(this);
		searchDItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuDept.add(searchDItem);
		
		listDItem = new JMenuItem("부서 목록");
		listDItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		listDItem.addActionListener(this);
		menuDept.add(listDItem);
		
		menuTitle = new JMenu("직급 정보");
		menuTitle.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuBar.add(menuTitle);
		
		addMItem = new JMenuItem("직급 추가");
		addMItem.addActionListener(this);
		addMItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuTitle.add(addMItem);
		
		updateMItem = new JMenuItem("직급 수정");
		updateMItem.addActionListener(this);
		updateMItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuTitle.add(updateMItem);
		
		deleteMItem = new JMenuItem("직급 삭제");
		deleteMItem.addActionListener(this);
		deleteMItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuTitle.add(deleteMItem);
		
		searchMItem = new JMenuItem("직급 검색");
		searchMItem.addActionListener(this);
		searchMItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuTitle.add(searchMItem);
		
		listMItem = new JMenuItem("직급 목록");
		listMItem.addActionListener(this);
		listMItem.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		menuTitle.add(listMItem);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == listMItem) {
			actionPerformedListMItem(e);
		}
		if (e.getSource() == searchMItem) {
			actionPerformedSearchMItem(e);
		}
		if (e.getSource() == deleteMItem) {
			actionPerformedDeleteMItem(e);
		}
		if (e.getSource() == updateMItem) {
			actionPerformedUpdateMItem(e);
		}
		if (e.getSource() == addMItem) {
			actionPerformedAddMItem(e);
		}
		if (e.getSource() == listEItem) {
			actionPerformedListEItem(e);
		}
		if (e.getSource() == searchItem) {
			actionPerformedSearchItem(e);
		}
		if (e.getSource() == deleteItem) {
			actionPerformedDeleteItem(e);
		}
		if (e.getSource() == listDItem) {
			actionPerformedListDItem(e);
		}
		if (e.getSource() == searchDItem) {
			actionPerformedSearchDItem(e);
		}
		if (e.getSource() == deleteDItem) {
			actionPerformedDeleteDItem(e);
		}
		if (e.getSource() == updateDItem) {
			actionPerformedUpdateDItem(e);
		}
		if (e.getSource() == addDItem) {
			actionPerformedAddDItem(e);
		}
		if (e.getSource() == updateItem) {
			actionPerformedUpdateItem(e);
		}
		if (e.getSource() == addItem) {
			actionPerformedAddItem(e);
		}
	}	
	private void revalidPanel(JPanel panel){
		contentPane.removeAll();
		contentPane.add(panel);
		revalidate();
	}
	
	//사원 검색, 삭제시 입력칸 비활성화
	private void EmpDisableTf(PanelEmployeeInputUI panel) { 
		panel.getpEmpName().getTf().setEditable(false);
		panel.getpTitle().getTf().setEditable(false);
		panel.getpManager().getTf().setEditable(false);
		panel.getpSalary().getTf().setEditable(false);
		panel.getpDeptNo().getTf().setEditable(false);
	}
	
//department
	protected void actionPerformedAddDItem(ActionEvent e) {//department 추가
		PanelDepartmentInputUI dept = new PanelDepartmentInputUI("추가", deptDao);
		dept.getLblAlram().setText("추가할 부서정보를 입력하십시오.");
		revalidPanel(dept);
	}
	protected void actionPerformedUpdateDItem(ActionEvent e) { //department 수정
		PanelDepartmentInputUI dept = new PanelDepartmentInputUI("수정", deptDao);
		dept.getLblAlram().setText("수정할 부서정보를 입력하십시오.");
		revalidPanel(dept);
	}
	protected void actionPerformedDeleteDItem(ActionEvent e) {
		PanelDepartmentInputUI dept = new PanelDepartmentInputUI("삭제", deptDao);
		dept.getLblAlram().setText("삭제할 부서번호를 입력하십시오.");
		dept.getpDeptName().getTf().setEditable(false);
		dept.getpFloor().getTf().setEditable(false);
		revalidPanel(dept);
	}
	protected void actionPerformedSearchDItem(ActionEvent e) {
		PanelDepartmentInputUI dept = new PanelDepartmentInputUI("검색", deptDao);
		dept.getLblAlram().setText("검색할 부서번호를 입력하십시오.");
		dept.getpDeptName().getTf().setEditable(false);
		dept.getpFloor().getTf().setEditable(false);
		revalidPanel(dept);
	}
	
	protected void actionPerformedListDItem(ActionEvent e) { //부서목록
		PanelListUI<Department> dept = new PanelListUI<Department>();
		dept.setDao(deptDao);
		contentPane.removeAll();
		contentPane.add(dept);
		contentPane.revalidate();
	}
	
//Employee
	protected void actionPerformedAddItem(ActionEvent e) {
		PanelEmployeeInputUI emp = new PanelEmployeeInputUI("추가", empDao);
		emp.getLblAlram().setText("추가할 사원정보를 입력하십시오.");
		revalidPanel(emp);
		
	}
	protected void actionPerformedUpdateItem(ActionEvent e) {
		PanelEmployeeInputUI emp = new PanelEmployeeInputUI("수정", empDao);
		emp.getLblAlram().setText("수정할 사원정보를 입력하십시오.");
		revalidPanel(emp);
	}		
	
	protected void actionPerformedDeleteItem(ActionEvent e) {
		PanelEmployeeInputUI emp = new PanelEmployeeInputUI("삭제", empDao);
		emp.getLblAlram().setText("삭제할 사원번호를 입력하십시오.");
		revalidPanel(emp);
		EmpDisableTf(emp);
	}
	
	protected void actionPerformedSearchItem(ActionEvent e) {
		PanelEmployeeInputUI emp = new PanelEmployeeInputUI("검색", empDao);
		emp.getLblAlram().setText("검색할 사원번호를 입력하십시오.");
		revalidPanel(emp);
		EmpDisableTf(emp);
	}
	protected void actionPerformedListEItem(ActionEvent e) { //사원목록
		PanelListUI<Employee> emp = new PanelListUI<Employee>();
		emp.setDao(empDao);
		contentPane.removeAll();
		contentPane.add(emp);
		contentPane.revalidate();
	}
	protected void actionPerformedAddMItem(ActionEvent e) {
		PanelCodeTitleInputUI ct = new PanelCodeTitleInputUI("추가", ctDao);
		ct.getLblAlarm().setText("추가할 직급전호를 입력하십시오.");
		revalidPanel(ct);
	}
	protected void actionPerformedUpdateMItem(ActionEvent e) {
		PanelCodeTitleInputUI ct = new PanelCodeTitleInputUI("수정", ctDao);
		ct.getLblAlarm().setText("수정할 직급전호를 입력하십시오.");
		revalidPanel(ct);
	}
	protected void actionPerformedDeleteMItem(ActionEvent e) {
		PanelCodeTitleInputUI ct = new PanelCodeTitleInputUI("삭제", ctDao);
		ct.getLblAlarm().setText("삭제할 직급전호를 입력하십시오.");
		ct.getpTitleName().getTf().setEditable(false);
		revalidPanel(ct);
	}	
	protected void actionPerformedSearchMItem(ActionEvent e) {
		PanelCodeTitleInputUI ct = new PanelCodeTitleInputUI("검색", ctDao);
		ct.getLblAlarm().setText("검색할 직급전호를 입력하십시오.");
		ct.getpTitleName().getTf().setEditable(false);
		revalidPanel(ct);
	}
	protected void actionPerformedListMItem(ActionEvent e) {
		PanelListUI<CodeTitle> ct = new PanelListUI<CodeTitle>();
		ct.setDao(ctDao);
		contentPane.removeAll();
		contentPane.add(ct);
		contentPane.revalidate();
	}
}
